//
//  MessageTableViewController.m
//  Recycling
//
//  Created by jzd on 2018/5/18.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "MessageTableViewController.h"
#import "MessageTableViewCell.h"
@interface MessageTableViewController ()

@property (assign,nonatomic) int page;
@property (strong,nonatomic)NSMutableArray *dataArray;
@end

@implementation MessageTableViewController

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BGCOLOR;
    self.navigationItem.title = @"消息记录";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MessageTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"msgCell"];
    self.tableView.allowsSelection = NO;
    
    //下拉刷新 上拉加载更多
    self.tableView.mj_header = [SJRefresh headerWithRefreshingTarget:self refreshingAction:@selector(requestNewData)];
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [SJRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreData)];
}

-(void)requestNewData{
    self.page = 1;
    [self.dataArray removeAllObjects];
    NSDictionary *params = @{
                             @"pageno":@(self.page),
                             @"limit" : @(pageNum),
                             @"wid":@([UserInfoTool shareUserInfoTool].Info.ID)
                             };
    [[SJNetWorkTool sharedTool]requestWithURLString:GetUserMsg parameters:params method:GET callBack:^(id responseObject, NSError *error) {
        if (responseObject) {
            int code = [[responseObject objectForKey:@"Code"] intValue];
            NSString *msg = [responseObject objectForKey:@"ErrorMessage"];
            id data = [responseObject objectForKey:@"Data"];
            if (code == 1) {
                if (![data isKindOfClass:[NSNull class]]) {
                    
                    int total = [[data objectForKey:@"total"] intValue];
                    if (total<pageNum) {
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                    NSArray *arr = [data objectForKey:@"rows"];
                    if (arr.count != 0) {
                        self.dataArray = arr.mutableCopy;
                    }else{
                        [self.tableView.mj_header endRefreshing];
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                        [self.navigationController.view showToast:@"暂无数据" completion:nil];
                        return ;
                    }
                    
                }else{
                    [self.navigationController.view showToast:@"暂无数据" completion:nil];
                }
            }else{
                [self.navigationController.view showToast:msg completion:nil];
            }
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
    }];
}
-(void)requestMoreData{
    self.page ++;
    NSDictionary *params = @{
                             @"pageno":@(self.page),
                             @"limit" : @(pageNum),
                             @"wid":@([UserInfoTool shareUserInfoTool].Info.ID)
                             };
    [[SJNetWorkTool sharedTool]requestWithURLString:GetUserMsg parameters:params method:GET callBack:^(id responseObject, NSError *error) {
        if (responseObject) {
            int code = [[responseObject objectForKey:@"Code"] intValue];
            NSString *msg = [responseObject objectForKey:@"ErrorMessage"];
            id data = [responseObject objectForKey:@"Data"];
            if (code == 1) {
                if (![data isKindOfClass:[NSNull class]]) {
                    
                    int total = [[data objectForKey:@"total"] intValue];
                    if (total<pageNum) {
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                    NSArray *arr = [data objectForKey:@"rows"];
                    if (arr.count != 0) {
                        [self.dataArray addObjectsFromArray:arr];
                    }else{
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                        [self.navigationController.view showToast:@"没有更多数据" completion:nil];
                        return ;
                    }
                    
                }else{
                    [self.navigationController.view showToast:@"暂无数据" completion:nil];
                }
            }else{
                [self.navigationController.view showToast:msg completion:nil];
            }
        }
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    }];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = [self.dataArray objectAtIndex:indexPath.row];
    MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"msgCell"];
    
    cell.titleN.text = [dict objectForKey:@"MsgTile"];
    cell.dateL.text = [dict objectForKey:@"CreateDate"];
    
    int msgType = [[dict objectForKey:@"MsgType"] intValue];
    
    if (msgType == 0) {
        cell.wangongType.text = @"接单";
    }else if (msgType == 1){
        cell.wangongType.text = @"完工扣款";
    }else{
        cell.wangongType.text = @"完工审核通过";
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

@end
